1. Field of the Invention
The present invention relates to digital data processing. More particularly, the present invention relates to a method and an apparatus for binary number conversion.
2. Description of the Related Art
Conversions between fixed-point binary numbers occur frequently in digital systems. When processing bits of two electronic devices in the same system are different, for example, a signal input into a video card is 5 bits but internal operation of the video card is 8 bits, the data processing between the input and internal operation needs bits conversion. These data in the video card usually represents colors, brightness, and coordinates. If some mistakes occur during bits conversion, it severely obstructs the normal operation of the video card.
The foregoing bits conversion of a binary number converts an m-bit binary number to an n-bit binary number. An unsigned m-bit binary number is able to represent an integer from 0 to 2m−1. An ideal conversion from an m-bit binary number to its n-bit counter part is to map 0 (the smallest value represented by m bits) to 0 (the smallest value represented by n bits), and to map 2m−1 (the largest value represented by m bits) to 2n−1 (the largest value represented by n bits). Besides, other values of the m-bit binary number also respectively and individually correspond to other values of the n-bit binary number, and it is a one-to-one ideal corresponding relation.
This one-to-one ideal corresponding relation is expressed by the equation
                    y        =                              f            ⁡                          (              x              )                                =                      [                                          x                ·                                                                            2                      n                                        -                    1                                                                              2                      m                                        -                    1                                                              +              0.5                        ]                                              (        1        )            where x is a value of an m-bit binary number, and y is a correct value of an n-bit binary number converted from m-bit binary number. The addition of 0.5 thereto means “round to nearest”. The symbol [I] represents the floor equation or Gauss equation whose value is the largest integer less than I. For example, a value of [3.4] is 3, and a value of [4.6] is 4. Combination of [I] and +0.5 therein can therefore accomplish rounding operation; i.e. a value of [3.4+0.5] is still 3, but a value of [4.6+0.5] becomes 5. From the foregoing description, the equation (1) linearly maps x=0 (the smallest value represented by m bits) to y=0 (the smallest value represented by n bits), and x=2m−1 (the largest value represented by m bits) to y=2n−1 (the largest value represented by n bits). Moreover, other values of x also respectively and individually map to other corresponding correct values of y.
However, modern digital systems do not use this ideal conversion method to convert a binary number between different bits, but rather use a method as illustrated in the following with reference to FIG. 1A to quickly convert a binary number between different bits without correctly rounding.
FIG. 1A is a schematic view of a conventional conversion method of a binary number between different bits. As illustrated in FIG. 1A, when an m-bit binary number 102 is converted to an n-bit binary number 104, the conversion method first supposesn=mq+r,  (2)
where 0≦r<m, and m·n·q·r are positive integers.
Then the binary number 102 is duplicated q times to obtain an m*q bits binary number. The m*q bits binary number is regarded as a most significant bits number 108 of the binary number 104. Further, the most significant r bits of the binary number 102 are copied to be a least significant bits number 106 of the binary number 104. The conventional method thus duplicates and combines the binary number as foregoing to obtain quickly the converted n-bit binary number 104 without a real numerical operation.
In FIG. 1A, a value of the binary number 102 is x, and a value of the binary number 104 converted thereby is y′, the y′ is an estimated value converted from the binary number 102, and is different from the correct value y. The conventional method obtains the binary number 104 without the real numerical operation, and the estimated value y′ can be represented by equation.
                              y          ′                =                              x            ⁡                          (                                                ∑                                      i                    =                    1                                    q                                ⁢                                                                  ⁢                                  2                                      r                    +                                          m                      ⁡                                              (                                                  q                          -                          1                                                )                                                                                                        )                                +                      [                          x                              2                                  m                  -                  r                                                      ]                                              (        3        )            
However, the value y′ obtained from equation (3) of the conventional conversion method is an estimated value, and may not be equal to the value y obtained from equation (1) of ideal conversion method. The values y and y′ of some values x respectively obtained from these two method are different. The following description cites an instance to interpret this inaccuracy, and uses a table to explain it in detail.
A 5-bit binary number 112 in FIG. 1B is converted to an 8-bit binary number 114 by using the conventional conversion method as illustrated in FIG. 1A. The 5-bit binary number 112 is composed of α, β, γ, δ, and ε, the 5 bits are arranged in order from the most significant bit to the least significant bit.
In FIG. 1B, the m and n are 5 and 8, respectively. The 5-bit binary number 112 needs to be duplicated once to be a most significant bits number 118 of the 8-bit binary number 114 during conversion. And the most significant r=3 bits α, β, and γ of the 5-bit binary number 112 are duplicated as a least significant number 116 of the 8-bit binary number 114. The foregoing conventional conversion method thus can obtain the 8-bit binary number 114, and α, β, γ, δ, ε, α, β, and γ thereof are arranged from the most significant bit to the least significant bit, as illustrated in FIG. 1B.
The description thereinafter describes several actual values x of the 5-bit binary number 112 are converted as several estimated values y′ of the 8-bit binary number 114, and compared with the correct values y thereof obtained from the ideal conversion method in Table. 1 to represent concretely errors made by the conventional conversion method. The 5-bit binary number 112 can used to represents 32 numbers from 0 (00000) to 31 (11111). These 32 numbers are respectively converted by ideal and conventional conversion method to obtain 32 8-bits numbers whose minimum value is (00000000) and maximum value is 255 (11111111).
In Table. 1, a value x is a value of the original 5-bit binary number. A correct value y is a value obtained by equation (1) of the ideal conversion method. An estimated value y′ is a value obtained by equation (3) of the conventional conversion method. A difference value e is a value of (y–y′), and the difference value e is represented by
                    e        =                              y            -                          y              ′                                =                                    [                                                x                  ⁢                                                                                    2                        3                                            -                      1                                                                                      2                        5                                            -                      1                                                                      +                0.5                            ]                        -                          [                              x                                  2                  2                                            ]                                                          (        4        )            
TABLE 1A comparison between the values of 5-bit binary numberand 8-binary number converted thereby.xyy′e0000188021616032524143333054141064949075857186666097474010828201190900129999013107107014115115015123123016132132017140140018148148019156156020165165021173173022181181023189189024197198−125206206026214214027222222028230231−1292392390302472470312552550
If the difference value e is equal to zero, the estimated value y′ and the correct value y are identical. The estimated value is the correct result, and additional modification is unnecessary. But if the difference value e is not equal to zero, the estimated value y′ is different from the correct value y, and some mistakes occur during bits conversion of a binary number between different bits.
As illustrated in Table. 1, when the value of the original 5-bit binary number 112 is 3, 7, 24, or 28, the estimated value y′ of the 8-bit binary 114 obtained by conventional conversion method is different from the correct value y obtained by ideal conversion method, respectively. When x is 3 or 7, the estimated value y′ is more than the correct value y with one; when x is 24 or 28, the estimated value y′ is less than the correct value y with one. The conventional conversion method therefore generates mistakes during conversion of a binary number between different bits when x is 3, 7, 24, or 28.
For digital data processing, a mistake during conversion of a binary number between different bits causes very serious problems. For example, these incorrect estimated values obtained by conventional conversion method generally are input values for a subsequent operation, and after many operations, the inaccuracy between the results of operating with incorrect estimated values and correct values becomes very large.
In an example of a video card, digital data usually represents colors, brightness, and/or coordinates. If mistakes occur during bits conversion, deviations of colors and coordinates may be caused. The deviation of colors makes colors of an image outputted from the video card incorrect, and the deviation of coordinates makes a graph plotted referring to these coordinates incorrect. Furthermore, the video card uses the converted values to query a table, or uses the converted values to be a reference coordinates to query colors. These queries generate mistakes due to these incorrect estimated values, and thus severely obstruct the normal operation of the video card.